package com.yanqu.road.server.channelserver.manager.activity.tonbing.activity;

import com.google.protobuf.AbstractMessage;
import com.yanqu.road.entity.activity.tonbingchannel.data.ChannelTonBingUserData;
import com.yanqu.road.server.channelserver.logic.ChannelMessageHelper;
import com.yanqu.road.server.channelserver.manager.user.ChannelUserMgr;

import java.util.*;

/**
 * 放一些配置的东西
 */
public class ChannelTonBingBase {
    List<Integer> channelIdList = new ArrayList<>();
    List<Long> serverIdlIst = new ArrayList<>();
    // ============================== 发消息 ==============================

    protected void sendChannelPacket(int code, AbstractMessage.Builder<?> messageBuilder, long serverId,int channelId) {
        ChannelMessageHelper.sendGamePacket(code, messageBuilder, channelId, serverId, 0);
    }

    protected void sendChannelPacket(int code, AbstractMessage.Builder<?> messageBuilder, ChannelTonBingUserData userData) {
        if(userData == null){
//            logger.error("跨域统兵发送消息没有玩家信息");
            return;
        }
        if(userData.getServerId() == 0){
//            logger.error("跨域统兵发送消息没有区服信息{}",userData.getUserId());
            return;
        }
        if(userData.getChannelId() == 0){
//            logger.error("跨域统兵发送消息没有渠道信息{}",userData.getUserId());
            return;
        }

        long userId = userData.getUserId();
        long serverId = userData.getServerId();
        int channelId = userData.getChannelId();
        /*UserBaseInfo userBaseInfo = ChannelUserMgr.getUserBaseInfo(userId);
        if (userBaseInfo == null) {
            return;
        }*/
//        YanQuMessage message = YanQuMessageUtils.buildMessage(code, messageBuilder);
//        MessageHelper.sendChannelPacket(userBaseInfo.getServerId(), userId, message);
        ChannelMessageHelper.sendGamePacket(code, messageBuilder, channelId, serverId, userId);
    }
    protected void sendChannelPacketToAll(int code, AbstractMessage.Builder<?> messageBuilde, Collection<Long> userIdList) {
        Map<Integer, Map<Long, List<Long>>> userIdListMap = new HashMap<>();

        ChannelTonBingActivity activity = null;
        if (this instanceof ChannelTonBingActivity) {
            activity = (ChannelTonBingActivity) this;
        }

        for (Long userId : userIdList) {
            int channelId = 0;
            long serverId = 0;
            if (activity != null) {
                ChannelTonBingUserData userData = activity.getUserData(userId);
                if (userData != null) {
                    channelId = userData.getChannelId();
                    serverId = userData.getServerId();
                }
            }

            if (channelId == 0) {
                List<Long> list = ChannelUserMgr.getUserCacheChannel(userId);
                if (list != null && list.size() > 1) {
                    channelId = list.get(0).intValue();
                    serverId = list.get(1);
                }
            }

            if (channelId == 0) {
                continue;
            }

            Map<Long, List<Long>> serverMap = userIdListMap.get(channelId);
            if (serverMap == null) {
                serverMap = new HashMap<>();
                userIdListMap.put(channelId, serverMap);
            }
            List<Long> userList = serverMap.get(serverId);
            if (userList == null) {
                userList = new ArrayList<>();
                serverMap.put(serverId, userList);
            }
            userList.add(userId);
        }

        //这里要改成每个渠道发一个包到指定活动跨服，在活动跨服再分发到游戏服
//        for (int channelId : channelIdList) {
//            for (Long serverId : serverIdlIst) {
//                if (ChannelActivityUnionMgr.isServerIdInChannel(serverId, channelId)) {
//                    sendChannelPacket(AgentProtocol.A_CTON_BING_TRANSFER, agentMsg, serverId, channelId);
//                }
//            }
//        }
        for (Map.Entry<Integer, Map<Long, List<Long>>> entry : userIdListMap.entrySet()) {

            int channelId = entry.getKey();
            Map<Long, List<Long>> serverMap = entry.getValue();

            ChannelMessageHelper.sendGamePacket(code, messageBuilde, channelId, serverMap);
        }
    }
}
